<?php

require (SERVER_ROOT_PATH . '/lib/funcs.php');


class User{
	
	private $param;
	private $db;
	private $mode;
	function __construct($mode="r",$debug=false)
	{	
		global $db_dudu_w,$db_dudu_r;
		$this->mode  = $mode;
		if ($this->mode=='w')
		{
			$this->param = paramSwap($db_dudu_w);
		}else{
			$this->param = paramSwap($db_dudu_r);
		}
		$this->db = new MysqlDB($this->param);
	}
	
	public function getUserByName($username){
		$sql="select * from user where username='$username'" ;
		$rs = $this->db->queryArray($sql);
		return $rs;
	}
	public function getUserByIdentity($identity){
		$sql="select * from user where identity='$identity'" ;
		$rs = $this->db->queryArray($sql);
		return $rs;
	}
	public function checkUsernameAndPwd($username,$pwd){
		$sql="select * from user where username='$username' and pwd='$pwd'" ;
		$rs = $this->db->queryArray($sql);
		return $rs;
	}
	public function checkUserExist($username){
		$sql="select * from user where username='$username' " ;
		$rs = $this->db->queryArray($sql);
		if(count($rs)==0){
			return false;
		}else{
			return true;
		}
	}
	public function insertUserByUsername($username,$pwd,$key){
		$sql="insert into user(username,pwd,identity,realname) values('$username','$pwd','$key','$username')";
		//echo $sql.'<br>';
		$rs=$rs = $this->db->query($sql);
		//echo 'res='.$rs.'<br>';
		return $rs;
	}
	public function getRecordById($id){
		$sql="select * from record where id=$id";
		$rs=$this->db->queryArray($sql);
		return $rs;
	}
	public function getRecordByUser($identity,$page=1,$size=10){
		$p=($page-1)*10;
		$sql="select * from record where identity='$identity' order by id asc limit $p,$size";
		$rs=$this->db->queryArray($sql);
		return $rs;
	}
	public function insertRecord($identity,$title,$content,$url){
		$sql="insert into record(identity,title,content,url) values('$identity','$title','$content','$url')";
		$rs=$rs = $this->db->query($sql);
		//echo 'res='.$rs.'<br>';
		return $rs;
	}
	public function closeDB(){
		$this->db->close();
	}
	
	
}
?>